package com.yiwei.hdb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.allen.library.SuperButton;
import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.base.ItemAdapter;
import com.yiwei.hdb.util.UI;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class V3AliPayRechargeMoneyAdapter extends ItemAdapter<String, V3AliPayRechargeMoneyAdapter.DefaultHolder> {


    private int selectPoi = -1;
    private String set_money;

    public V3AliPayRechargeMoneyAdapter(Context context) {
        super(context);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_recharge_money_v3, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        holder.setIsRecyclable(false);
        String item = getItem(position);


        if (selectPoi == position && position != getItemCount() - 1) {
            holder.txtRecharge.setTextColor(0xffffffff);
            holder.txtRecharge.setShapeGradientOrientation(SuperButton.LEFT_RIGHT).setUseShape();
        } else {
            holder.txtRecharge.setTextColor(0xff00C3BE);
            holder.txtRecharge.setShapeGradientOrientation(-1).setUseShape();
        }

        if (item.equals("0")) {
            holder.txtRecharge.setText("自定义");
            holder.editMoney.setEnabled(true);
            holder.editMoney.setVisibility(View.VISIBLE);
        } else {
            holder.txtRecharge.setText("充值" + item + "元");
            holder.editMoney.setEnabled(false);
            holder.editMoney.setVisibility(View.GONE);
        }
        holder.txtRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPoi = position;
                if (onChooseListener != null) {
                    if (position == getItemCount() - 1) {
//                        onChooseListener.onClick(set_money);
                    } else {
                        onChooseListener.onClick(item);

                    }
                }
                notifyDataSetChanged();
            }
        });

        holder.editMoney.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    selectPoi = position;
                    holder.txtRecharge.setText("");
                } else {
                    if (TextUtils.isEmpty(UI.toString(holder.editMoney))) {
                        holder.txtRecharge.setText("");
                    } else {
                        holder.txtRecharge.setText("自定义");
                    }

                }
            }
        });

        holder.editMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                set_money = UI.toString(holder.editMoney);
                onChooseListener.onClick(set_money);
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_recharge)
        SuperButton txtRecharge;
        @BindView(R.id.edit_money)
        EditText editMoney;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String money);
    }
}
